home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d7 / lanuts.arc / SENDUSER.C < prev    next >
Text File  |  1991-10-30  |  7KB  |  235 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <direct.h>
  6. #include <conio.h>
  7.  
  8. #include "lantasti.h"
  9.  
  10. #define DOS 0x21
  11. #define NETBIOS 0x5C
  12. #define DELIM "+, "
  13.  
  14. /* structure to allow easy access to both segment and offset portions of
  15.    far pointers */
  16. #define LIST_SIZE 50
  17. #define NAME_SIZE 20
  18. typedef struct LIST {
  19.     int  num_servers;
  20.     char server[LIST_SIZE][NAME_SIZE];
  21.        } LIST;  
  22. char found;
  23. /* definitions to save typing time */
  24. #define C_SERVER list->server[i % list->num_servers]
  25. struct msg_buffer msg;
  26. struct SREGS segregs;
  27. union REGS registers;
  28. struct SEGOFFS {
  29.   unsigned offs;
  30.   unsigned seg;
  31. };
  32. typedef union POINTER {
  33.   unsigned char *ptr;
  34.   struct SEGOFFS l;
  35. } POINTER;        
  36. union POINTER ptr;
  37.  
  38. /***********************************************************************
  39.  Delete leading and trailing blanks.
  40. ***********************************************************************/
  41. void dltb(string)
  42.   char *string;
  43. {
  44.   int i;
  45.  
  46.   i  = strspn(string, " ");
  47.  
  48.   if (i) strcpy(string,string + i);
  49.  
  50.   i = strlen(string) - 1;
  51.   while (i && (string[i] == ' ')) {
  52.     string[i--] = '\0';
  53.   }
  54. }
  55.  
  56.  
  57. /* getstring ********************************************************************
  58.  Get a string from the console -- takes a pointer to a string buffer, the 
  59.  longest permissible length and two switches. The empty switch determines
  60.  whether or not the user is allowed to enter an empty string. If TRUE, he
  61.  can, if FALSE, he must enter something.  The shown switch determines 
  62.  whether or not input is echoed to the screen, TRUE if echoed, FALSE if
  63.  invisible
  64. *****************************************************************************/
  65. void getstring(prompt,buffer,length,empty,shown) 
  66.   char *prompt,*buffer;
  67.   int length,empty,shown;
  68. {
  69.   fprintf(stdout,"%s",prompt);
  70.   fgets(buffer,length,stdin);
  71.   if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
  72.   while (!(strlen(buffer) || empty)) {
  73.     fprintf(stdout,"%s",prompt);
  74.     fgets(buffer,length,stdin);
  75.     if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
  76.   }
  77. }
  78.  
  79. /* error ********************************************************************
  80. Traps for system errors and prints a happy message.
  81. *****************************************************************************/
  82. void error(code,message)
  83.   int code;
  84.   char *message;
  85. {
  86.   char *ptr;
  87.   
  88.   if (code) {
  89.     ptr = get_error_text(code);
  90.     puts(ptr);
  91.   }
  92.   else puts(message);
  93. }
  94.  
  95. /* get_active_servers ********************************************************************
  96. Gets a list of the active servers.
  97. *****************************************************************************/
  98. void get_active_servers(list)
  99.   LIST *list;
  100. {
  101.   char buffer[17];
  102.   int index,result;
  103.   
  104.   index = 0;
  105. /* pull in the name of a server */
  106.   while ((result = get_active_server(buffer,index++))) {
  107.     sprintf(list->server[list->num_servers++],"\\\\%s",buffer);
  108. /* only allow a reasonable number of servers */
  109.     if (list->num_servers >= LIST_SIZE) {
  110.       puts("Warning: Too many servers.  Only the first 50 can be used.");
  111.       break;
  112.     }
  113.   }
  114. }
  115. void display_help() {
  116.   puts("SENDUSER utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  117.   puts("All rights reserved.  LANtastic is a trademark of Artisoft, Inc.\n");
  118.   puts("Usage: SENDUSER <user name(s)> <message> /[OPTIONS]");
  119.   puts("  A question mark in the list causes the program to prompt the user");
  120.   puts("  for input.  SENDUSER with no arguments will display help.");
  121.   puts("Available options are:");
  122.   puts("/HELP - display this documentation");
  123.   exit(0);
  124. }  
  125.  
  126. /* scan_command_line ********************************************************************
  127. Scans the command line for /options like HELP.  Doesn't do anything else.
  128. *****************************************************************************/
  129. scan_command_line(argc,argv)
  130.   int argc;
  131.   char *argv[];
  132. {
  133.   int i;
  134.  
  135. /* for each argument */
  136.   if (argc < 3) {
  137.     display_help();
  138.     exit(0);
  139.   }  
  140.   for (i = 1;i < argc; i++) {
  141. /* convert to upper case and see if it exists */
  142.     strupr(argv[i]);
  143.     if (argv[i] !=NULL) {
  144. /* if it's /HELP, run our happy message */
  145.       if (!strcmp(argv[i],"/HELP")) {
  146.         display_help();
  147.         break;
  148.       }
  149.     }
  150.   }
  151. }
  152. /* do_userlist ********************************************************************
  153. Breaks apart the names and checks them against the list of logged in folks.
  154. Prints its answer.
  155. *****************************************************************************/
  156. int senduser(list,username,message)
  157.   LIST *list;
  158.   char *username,*message;
  159. {
  160.   int result,i,j;
  161.   char *tok_ptr;
  162.   ACTIVE_USER user;
  163.   char machine[20];
  164.  
  165.   machine[0] = 0;
  166.   if (username[0] == '?')
  167.     getstring("User name(s): ",username,80,FALSE,TRUE);
  168.   
  169.   strcpy(msg.text,message);  
  170.   if (msg.text[0] == '?')
  171.     getstring("Message: ",msg.text,80,FALSE,TRUE);  
  172.   
  173. /* no matter what else happens, break off a name and check it */
  174.   tok_ptr = strtok(username,DELIM);
  175.   ptr.ptr = (char *) &msg;
  176.   while (tok_ptr != NULL) {
  177.     for (i = 0; i < list->num_servers; i++) {
  178.       j = 0;
  179.       dltb(list->server[i]);
  180.       while (!(result = (get_user_info(list->server[i],j++,&user)))) {
  181.         dltb(user.name);
  182.         if (!strcmpi(tok_ptr,user.name)) {
  183.           found = TRUE;
  184.           dltb(user.machine);
  185.           if (!strcmpi(machine,user.machine)) continue;
  186.           strcpy(msg.destination,user.machine);
  187.           segregs.ds = ptr.l.seg;
  188.           registers.x.si = ptr.l.offs;
  189.           registers.x.ax = 0x5f98;
  190.           intdosx(®isters,®isters,&segregs);
  191.           strcpy(machine,user.machine);
  192.           printf("Message sent to machine %s.\n",user.machine);
  193.           break;
  194.         }
  195.       }
  196.     }
  197.     if (!found) printf("User %s is not logged in.\n",username);
  198.     found = FALSE;
  199.     tok_ptr = strtok(NULL,DELIM);  
  200.   }
  201. }
  202.  
  203. /* main ********************************************************************
  204.  
  205. *****************************************************************************/
  206. int main(argc,argv)
  207.   int argc;
  208.   char *argv[];
  209. {
  210.   LIST  list;        /*server, user and password lists*/ 
  211.   int i;
  212.   char username[80];
  213.  
  214. /* scan for /HELP option */
  215.   found = FALSE;
  216.   scan_command_line(argc,argv);
  217.   
  218. /* fetch list of servers -- we assume you want to know for all */
  219.   list.num_servers = 0;
  220.   get_active_servers(&list);
  221.   
  222.   /* if there aren't any servers, bomb out. */
  223.   if (list.num_servers <= 0) {
  224.     error(FALSE,"You are not logged in to any servers.");
  225.     return(0);
  226.   }
  227.  
  228. /* otherwise, for each name, see if they're logged in and where. */
  229.   for (i = 1; i < argc-1; i++)
  230.     senduser(&list,argv[i],argv[argc-1]);
  231.   
  232. /* outta there */
  233.   return(0);
  234. }
  235.